<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="stylesheet" href="css/backgroundAnimation.css">
  <link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <title>题海</title>
</head>
<body>
<!-- 背景方块 -->
<ul class="bg-squares">
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>

<!-- 登录表单 -->
<section class="container">
  <h2>欢迎来到<b class="art-text">题海</b></h2>

  <!-- 错误信息 -->
  <div id="error-message" class="error-message" style="display: none;"></div>

  <!-- 登录表单 -->
  <form id="login-form">
    <div class="form">
      <input type="text" id="account" name="account" placeholder="账号/手机号" required>
      <input type="password" id="password" name="password" placeholder="密码" required>
      <div class="form-actions">
        <a href="html/enroll.html" class="btn-login">注册</a>
        <button type="submit" class="btn-login">登录</button>
      </div>
    </div>
  </form>
</section>
<script>
  // 处理登录表单提交
  document.getElementById('login-form').addEventListener('submit', function (event) {
    event.preventDefault(); // 阻止表单默认提交行为

    const formData = new FormData(event.target);
    formData.append("action", "login"); // 添加 action 参数

    axios.post('/TiHai/users', new URLSearchParams(formData), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
      }
    })
            .then(response => {
              if (response.data.success) {
                window.location.href = 'html/Main.html'; // 登录成功，跳转到主页面
              } else {
                alert(response.data.message); // 登录失败，显示错误信息
              }
            })
  });
</script>
</body>
</html>